package com.odin.backgrounddownload.downloader;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;

import androidx.core.app.NotificationCompat;

import com.odin.backgrounddownload.Logger;
import com.odin.backgrounddownload.R;

public class SGNotificationHelper
{
    private final String m_ChannelId ="odin_download_channel";
    private final int m_NotifyId = 666;
    private String m_AppName;
    private String getAppName()
    {
        if (m_AppName == null)
        {
            PackageManager packageManager = m_UnityMainActivity.getApplicationContext().getPackageManager();

            try {
                ApplicationInfo applicationInfo = packageManager.getApplicationInfo(m_UnityMainActivity.getPackageName(), 0);
                m_AppName = (String) packageManager.getApplicationLabel(applicationInfo);
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
        }
        return m_AppName;
    }

    private Bitmap m_AppIcon;
    public Bitmap getAppIcon()
    {
        if (m_AppIcon == null)
        {
            PackageManager packageManager = m_UnityMainActivity.getApplicationContext().getPackageManager();
            try {
                ApplicationInfo applicationInfo = packageManager.getApplicationInfo(m_UnityMainActivity.getPackageName(), 0);
                Drawable appIcon = packageManager.getApplicationIcon(applicationInfo);
                m_AppIcon = ((BitmapDrawable) appIcon).getBitmap();
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
        }
        return m_AppIcon;
    }

    private Activity m_UnityMainActivity;

    private NotificationManager m_NotificationManager;

    public SGNotificationHelper(Activity activity)
    {
        m_UnityMainActivity = activity;
        m_NotificationManager = (NotificationManager) m_UnityMainActivity.getSystemService(Context.NOTIFICATION_SERVICE);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {
            NotificationChannel channel = new NotificationChannel(m_ChannelId, "Odin", NotificationManager.IMPORTANCE_LOW);//生成channel
            m_NotificationManager.createNotificationChannel(channel);//添加channel
        }
    }

    public void CancelNotification()
    {
        if(m_NotificationManager == null) return;

        m_NotificationManager.cancel(m_NotifyId);
    }

    public void UpdateNotification(String content)
    {
        if (m_NotificationManager==null)
        {
            return;
        }

        NotificationCompat.Builder builder = new NotificationCompat.Builder(m_UnityMainActivity);
        try
        {
            builder.setContentTitle(getAppName());
            builder.setContentText(content);
            builder.setSmallIcon(R.drawable.notification_icon);
            builder.setLargeIcon(getAppIcon());
            builder.setWhen(System.currentTimeMillis());
            builder.setAutoCancel(true);
            PendingIntent contentIntent = PendingIntent.getActivity(m_UnityMainActivity, 0, m_UnityMainActivity.getIntent(), PendingIntent.FLAG_IMMUTABLE);
            builder.setContentIntent(contentIntent);
            if(Build.VERSION.SDK_INT >= 26)
            {
                builder.setCategory(Notification.CATEGORY_MESSAGE);
                builder.setChannelId(m_ChannelId);
            }
            //发送通知请求
            m_NotificationManager.notify(m_NotifyId, builder.build());
        }
        catch (Exception e)
        {
            Logger.d("J后台下载，推送失败 : " + e.getMessage());
        }
    }

}
